package com.nosnow.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nosnow.common.utils.R;
import com.nosnow.statistics.entity.StatisticsDaily;
import com.nosnow.statistics.invoking.UcenterClient;
import com.nosnow.statistics.mapper.StatisticsDailyMapper;
import com.nosnow.statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author bingheng
 * @since 2020-05-31
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        // 添加记录之前删除表中相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        // 远程调用：获取某一天的注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer)registerR.getData().get("countRegister");

        // 将获取到的数据添加到统计分析表即可
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setDateCalculated(day);

        // TODO 这里仅统计了注册人数，其他属性统计，后期完善，这里以随机数代替
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100, 200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100, 200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getshowData(String type, String begin, String end) {
        // 根据条件查询对应的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);

        // 数据处理
        List<String> dateList = list.parallelStream().map(StatisticsDaily::getDateCalculated).collect(Collectors.toList());
        List<Integer> numList = new ArrayList<>();
        switch (type) {
            case "login_num" :
                numList = list.parallelStream().map(StatisticsDaily::getLoginNum).collect(Collectors.toList());
                break;
            case "register_num" :
                numList = list.parallelStream().map(StatisticsDaily::getRegisterNum).collect(Collectors.toList());
                break;
            case "course_num" :
                numList = list.parallelStream().map(StatisticsDaily::getCourseNum).collect(Collectors.toList());
                break;
            case "video_view_num" :
                numList = list.parallelStream().map(StatisticsDaily::getVideoViewNum).collect(Collectors.toList());
                break;
            default:
                break;
        }

        // 封装返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", dateList);
        map.put("numDataList", numList);
        return map;
    }
}
